import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/app_permission/permission_widget.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_button.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_observer_list/site_observer_list_controller.dart';
import 'package:yt_devops/pages/site/site_observer_list/views/dynamic_monitor_widget.dart';
import 'package:yt_devops/pages/site/site_observer_list/views/ess_monitor.dart';
import 'package:yt_devops/pages/site/site_observer_list/views/my_top_tabbar.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';

class SiteObserverListView extends GetView<SiteObserverListController> {
  const SiteObserverListView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // // 市电监测
    // String allCountStr = ResKey.siteSettingObserverElectricityObserver.tr;
    // int allId = 0;
    // // 光伏监测
    // String powerGenerationCountStr =
    //     ResKey.siteSettingObserverPhotovoltaicObserver.tr;
    // int powerGenerationId = 1;
    // // 储能监测
    // String storedEnergyCountStr =
    //     ResKey.siteSettingObserverStoreEnergyObserver.tr;
    // int storedEnergyId = 2;
    // // 充电桩监测
    // String powerConsumptionCountStr =
    //     ResKey.siteSettingObserverChargeObserver.tr;
    // int powerConsumptionId = 3;
    // // 其他负载监测
    // String otherUnitCountStr = ResKey.siteSettingObserverOtherLoadObserver.tr;
    // int otherUnitId = 4;
    //
    // List<int> subSystemId = [
    //   allId,
    //   powerGenerationId,
    //   storedEnergyId,
    //   powerConsumptionId,
    //   otherUnitId
    // ];

    return GetBuilder<SiteObserverListController>(
      builder: (controller) {
        bool isShowSaveBtn = true;
        if (controller.tabController.index == 1) {
          if (controller.photovoltaicDataMap['flag'] != null &&
              controller.photovoltaicDataMap['flag'] == true) {
            isShowSaveBtn = true;
          } else {
            isShowSaveBtn = false;
          }
        }
        if (controller.tabController.index == 2) {
          if (controller.energyDataMap['flag'] != null &&
              controller.energyDataMap['flag'] == true) {
            isShowSaveBtn = true;
          } else {
            isShowSaveBtn = false;
          }
        }
        if (controller.tabController.index == 3) {
          if (controller.chargeDataMap['flag'] != null &&
              controller.chargeDataMap['flag'] == true) {
            isShowSaveBtn = true;
          } else {
            isShowSaveBtn = false;
          }
        }
        if (controller.tabController.index == 4) {
          if (controller.otherDataMap['flag'] != null &&
              controller.otherDataMap['flag'] == true) {
            isShowSaveBtn = true;
          } else {
            isShowSaveBtn = false;
          }
        }
     return Scaffold(
        resizeToAvoidBottomInset: true,
        appBar: AppBarWidget.defaultAppBar(ResKey.siteOverviewRunObserver.tr),
        body: Column(
          children: [
            Container(
              color: Colors.white,
              padding: const EdgeInsets.only(left: 6, right: 6),
              child: MyTopTabBar(controller.tabController, controller.tabs),
            ),
            Expanded(
              child: SizedBox(
              child: TabBarView(
                controller: controller.tabController,
                children: [
                  _tableViewItems(context, 0),
                  _tableViewItems(context, 1),
                  // _tableViewItems(context, 2),
                  EssMonitorWidget(

                    controller.energyDataMap,
                    2,
                    controller.siteId,
                    key: controller.childKey,
                      (){
                      controller.update();
                      }
                  ),
                  _tableViewItems(context, 3),
                  _tableViewItems(context, 4),
                ],
              ),
            )),
          ],
        ),
        bottomNavigationBar: PermissionWidget(permissionStringList: const [PermissionConstant.homeSiteManageSettingMonitorSettingSave],
        child: Visibility(
          visible: isShowSaveBtn,
          child: Container(
            color: Colour.white,

            padding: EdgeInsets.only(
                left: 16,
                right: 16,
                top: 16,
                bottom: ScreenUtil().bottomBarHeight + 5),
            child: YtButton(
              enabled: true,
              color: Colour.primaryColor,
              text: ResKey.save.tr,
              onPress: () => controller.handleSave(),
            ),
          ),
        )),
      );

      }
    );
  }

  _tableViewItems(BuildContext context, int index) {
    var map = {};
    if (index == 0) {
      map = controller.mainsDataMap;
    } else if (index == 1) {
      map = controller.photovoltaicDataMap;
    } else if (index == 2) {
      map = controller.energyDataMap;
    } else if (index == 3) {
      map = controller.chargeDataMap;
    } else if (index == 4) {
      map = controller.otherDataMap;
    }
    return ListView(
      children: [
        Visibility(
          visible: index != 0,
          child: Container(
            margin: const EdgeInsets.only(top: 10,left: 16,right: 16),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: Colors.white,
            ),
            padding: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0),
            height: 50,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(ResKey.siteSettingObserverPhotovoltaicObserverStatus.tr,
                    style:
                        ytTextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
                CupertinoSwitch(
                    activeColor: Colour.primaryColor,
                    value: (map['flag'] != null && map['flag'] == true)
                        ? true
                        : false,
                    onChanged: map['isCanSwitch'] != null && map['isCanSwitch'] == true?(isOpen) {
                      if (!isOpen) {
                        BrnDialogManager.showConfirmDialog(
                          context,
                          cancel: ResKey.cancel.tr,
                          confirm: ResKey.confirm.tr,
                          title: ResKey.siteSettingObserverTurnoff.tr,
                          message: ResKey.siteSettingObserverTurnoffTip.tr,
                          onCancel: () => Get.back(),
                          onConfirm: () {
                            Log.d('发送网络请求关闭页面');
                            controller.obsStatusChange(isOpen, map);
                            Get.back();
                          },
                        );
                      } else {
                        controller.obsStatusChange(isOpen, map);
                      }
                    }:null)
              ],
            ),
          ),
        ),
        // Gaps.vGap10,
        DynamicMonitorWidget(
          map,
          index,
          (dataList, type) => controller.tapEditHandler(dataList, type),
          editClick: () {
            controller.update();
          },
        ),
        // _dynamicWidget(map, index),
        const Divider(
          height: 20,
        ),
      ],
    );
  }
}
